package com.yxq.mdt.app.modules.auction.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.orhanobut.hawk.Hawk;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.modules.auction.adapter.AuctionProfitAdapter;
import com.yxq.mdt.app.modules.auction.bean.AuctionList;
import com.yxq.mdt.app.modules.auction.bean.AuctionProfitBean;
import com.yxq.mdt.app.modules.auction.bean.AuctionTxBean;
import com.yxq.mdt.app.modules.auction.bean.HonorValueBean;
import com.yxq.mdt.app.modules.auction.model.AuctionModel;
import com.yxq.mdt.app.modules.auction.presenter.AuctionContract;
import com.yxq.mdt.app.modules.auction.presenter.AuctionPresenter;
import com.yxq.mdt.app.modules.main.bean.NameAndPhoneBean;
import com.yxq.mdt.app.modules.webview.ui.MiWebViewActivity;
import com.yxq.mdt.app.net.ApiModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的收益
 */
public class AuctionProfitAc extends BaseActivity<AuctionPresenter> implements AuctionContract.View {
    @BindView(R.id.titlebar)
    CommonTitleBar titlebar;
    @BindView(R.id.auction_cash_money)
    TextView auctionCashMoney;
    @BindView(R.id.auction_withdrawal)
    TextView auctionWithdrawal;
    @BindView(R.id.auction_withdrawal_list)
    TextView auctionWithdrawalList;
    @BindView(R.id.auction_withdrawal_cumulative)
    TextView auctionWithdrawalCumulative;
    @BindView(R.id.auction_withdrawal_cumulative_money)
    TextView auctionWithdrawalCumulativeMoney;
    @BindView(R.id.auction_recyclerView)
    RecyclerView auctionRecyclerView;
    AuctionProfitAdapter auctionProfitAdapter;
    AuctionProfitAdapter auctionProfitAdapterOver;
    AuctionProfitAdapter auctionProfitAdapterPre;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.auction_is_show_team)
    LinearLayout auctionIsShowTeam;
    @BindView(R.id.auction_is_show_profit)
    LinearLayout auctionIsShowProfit;
    @BindView(R.id.auction_regimental_commander)
    TextView auctionRegimentalCommander;
    @BindView(R.id.auction_is_lint)
    LinearLayout auctionIsLint;
    @BindView(R.id.layout_auction_withdraw)
    LinearLayout layoutAuctionWithdraw;
    @BindView(R.id.tv_or)
    TextView tvOr;
    @BindView(R.id.auction_withdrawal_cumulative_all)
    TextView auctionWithdrawalCumulativeAll;
    @BindView(R.id.auction_withdrawal_cumulative_money_all)
    TextView auctionWithdrawalCumulativeMoneyAll;
    @BindView(R.id.auction_withdrawal_cumulative_pre)
    TextView auctionWithdrawalCumulativePre;
    @BindView(R.id.auction_withdrawal_cumulative_money_pre)
    TextView auctionWithdrawalCumulativeMoneyPre;
    @BindView(R.id.auction_auction)
    TextView auctionAuction;
    int type = -1;
    @BindView(R.id.auction_profit_layout_all)
    LinearLayout auctionProfitLayoutAll;
    @BindView(R.id.auction_profit_layout_over)
    LinearLayout auctionProfitLayoutOver;
    @BindView(R.id.auction_profit_layout_pre)
    LinearLayout auctionProfitLayoutPre;
    @BindView(R.id.auction_recyclerViewOver)
    RecyclerView auctionRecyclerViewOver;
    @BindView(R.id.auction_recyclerViewPre)
    RecyclerView auctionRecyclerViewPre;
    int pageOver = 1;
    int pagePre = 1;
    @BindView(R.id.iv_line_tab_fans)
    ImageView ivLineTabFans;
    @BindView(R.id.iv_line_tab_fans_over)
    ImageView ivLineTabFansOver;
    @BindView(R.id.iv_line_tab_fans_pre)
    ImageView ivLineTabFansPre;
    @BindView(R.id.auction_regimental_commander_pre)
    TextView auctionRegimentalCommanderPre;

    @Override
    public int getLayoutId() {
        return R.layout.activity_auction_profit_activity;
    }

    @Override
    public void initView() {
        mPresenter = new AuctionPresenter(this, new AuctionModel());

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        auctionRecyclerView.setLayoutManager(linearLayoutManager);
        auctionProfitAdapter = new AuctionProfitAdapter(R.layout.item_auction_cash, null);
        auctionRecyclerView.setAdapter(auctionProfitAdapter);

        linearLayoutManager = new LinearLayoutManager(mActivity);
        auctionRecyclerViewOver.setLayoutManager(linearLayoutManager);
        auctionProfitAdapterOver = new AuctionProfitAdapter(R.layout.item_auction_cash, null);
        auctionRecyclerViewOver.setAdapter(auctionProfitAdapterOver);

        linearLayoutManager = new LinearLayoutManager(mActivity);
        auctionRecyclerViewPre.setLayoutManager(linearLayoutManager);
        auctionProfitAdapterPre = new AuctionProfitAdapter(R.layout.item_auction_cash, null);
        auctionRecyclerViewPre.setAdapter(auctionProfitAdapterPre);

        refreshLayout.setOnRefreshListener(refreshLayout -> {
            switch (type) {
                case 1://已到账
                    pageOver = 1;
                    mPresenter.getUserIncomeList(30, pageOver, type);
                    break;
                case 0:
                    pagePre = 1;
                    mPresenter.getUserIncomeList(30, pagePre, type);
                    break;
                default:
                    page = 1;
                    mPresenter.getUserIncomeList(30, page, type);
                    break;
            }

            status();

        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {

            switch (type) {
                case 1://已到账
                    pageOver++;
                    mPresenter.getUserIncomeList(30, pageOver, type);
                    break;
                case 0:
                    pagePre++;
                    mPresenter.getUserIncomeList(30, pagePre, type);
                    break;
                default:
                    page++;
                    mPresenter.getUserIncomeList(30, page, type);
                    break;
            }
        });

    }

    /**
     * 收益状态
     */
    private void status() {
        AuctionModel.getHonorValue(new ApiModel.CommonCallback<HonorValueBean>() {
            @Override
            public void onSuccess(HonorValueBean honorValueBean) {

                if (honorValueBean.getIncome().equals("0")) {
                    //无收益
                    auctionIsLint.setVisibility(View.VISIBLE);
                    layoutAuctionWithdraw.setVisibility(View.GONE);
                    tvOr.setVisibility(View.VISIBLE);
                    auctionIsShowTeam.setVisibility(View.VISIBLE);
                    auctionIsShowProfit.setVisibility(View.VISIBLE);

                    if (honorValueBean.getPm_level() == 0) {
                        //无收益不是团长
                    } else if (honorValueBean.getPm_level() == 1) {
                        //无收益是团长
                        tvOr.setVisibility(View.GONE);
                        auctionIsShowTeam.setVisibility(View.GONE);
                    } else if (honorValueBean.getPm_level() == 3) {
                        //无收益是预备团长
                        auctionRegimentalCommanderPre.setVisibility(View.GONE);
                    }
                } else {
                    //有收益
                    auctionIsLint.setVisibility(View.GONE);
                    layoutAuctionWithdraw.setVisibility(View.VISIBLE);
                    tvOr.setVisibility(View.GONE);
                    auctionIsShowTeam.setVisibility(View.GONE);
                    auctionIsShowProfit.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(String errMsg) {

            }
        });
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        AuctionModel.getUserIncomeList(30, 1, "1", new ApiModel.CommonCallback<AuctionTxBean>() {
            @Override
            public void onSuccess(AuctionTxBean auctionTxBean) {
                auctionProfitAdapterOver.setNewData(auctionTxBean.getPmUserIncomeList().getList());
            }

            @Override
            public void onError(String errMsg) {

            }
        });
        AuctionModel.getUserIncomeList(30, 1, "0", new ApiModel.CommonCallback<AuctionTxBean>() {
            @Override
            public void onSuccess(AuctionTxBean auctionTxBean) {
                auctionProfitAdapterPre.setNewData(auctionTxBean.getPmUserIncomeList().getList());
            }

            @Override
            public void onError(String errMsg) {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshLayout.autoRefresh();
    }

    @OnClick({R.id.auction_profit_layout_all, R.id.auction_profit_layout_over, R.id.auction_profit_layout_pre, R.id.auction_withdrawal, R.id.auction_withdrawal_list, R.id.auction_recyclerView, R.id.auction_regimental_commander, R.id.auction_regimental_commander_pre, R.id.auction_auction})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //提现
            case R.id.auction_withdrawal:
                Intent it = new Intent(mActivity, AuctionCashAc.class);
                startActivity(it);
                break;
            //提现记录
            case R.id.auction_withdrawal_list:
                Intent profit = new Intent(mActivity, AuctionProfitListAc.class);
                startActivity(profit);
                break;
            case R.id.auction_regimental_commander:
                //申请成为团长
                it = new Intent(mActivity, BecomeTeamAc.class);
                startActivity(it);
                break;
            case R.id.auction_regimental_commander_pre:
                //申请成为预备团长
                it = new Intent(mActivity, PreTeamLeaderIntroActivity.class);
                startActivity(it);
                break;
            case R.id.auction_auction:
                NameAndPhoneBean nameAndPhoneBean = Hawk.get(SPKey.SP_KEY_NAME_PHONE_INFO);
                //参与活动
                it = new Intent(mActivity, MiWebViewActivity.class);
                it.putExtra("url", nameAndPhoneBean.getPmSetting().getPm_url());
                startActivity(it);
                break;
            case R.id.auction_profit_layout_all://总收益
                auctionWithdrawalCumulativeAll.setTextColor(getResources().getColor(R.color.holo_red_light));
                auctionWithdrawalCumulativeMoneyAll.setTextColor(getResources().getColor(R.color.holo_red_light));
                auctionWithdrawalCumulative.setTextColor(getResources().getColor(R.color.color_font_black));
                auctionWithdrawalCumulativeMoney.setTextColor(getResources().getColor(R.color.color_font_black));
                auctionWithdrawalCumulativePre.setTextColor(getResources().getColor(R.color.color_font_black));
                auctionWithdrawalCumulativeMoneyPre.setTextColor(getResources().getColor(R.color.color_font_black));
                type = -1;
//                refreshLayout.autoRefresh();
                auctionRecyclerView.setVisibility(View.VISIBLE);
                auctionRecyclerViewOver.setVisibility(View.GONE);
                auctionRecyclerViewPre.setVisibility(View.GONE);
                ivLineTabFans.setVisibility(View.VISIBLE);
                ivLineTabFansOver.setVisibility(View.INVISIBLE);
                ivLineTabFansPre.setVisibility(View.INVISIBLE);
                break;
            case R.id.auction_profit_layout_over://已到账
                auctionWithdrawalCumulative.setTextColor(getResources().getColor(R.color.holo_red_light));
                auctionWithdrawalCumulativeMoney.setTextColor(getResources().getColor(R.color.holo_red_light));
                auctionWithdrawalCumulativeAll.setTextColor(getResources().getColor(R.color.color_font_black));
                auctionWithdrawalCumulativeMoneyAll.setTextColor(getResources().getColor(R.color.color_font_black));
                auctionWithdrawalCumulativePre.setTextColor(getResources().getColor(R.color.color_font_black));
                auctionWithdrawalCumulativeMoneyPre.setTextColor(getResources().getColor(R.color.color_font_black));
                type = 1;
//                refreshLayout.autoRefresh();
                auctionRecyclerView.setVisibility(View.GONE);
                auctionRecyclerViewOver.setVisibility(View.VISIBLE);
                auctionRecyclerViewPre.setVisibility(View.GONE);
                ivLineTabFans.setVisibility(View.INVISIBLE);
                ivLineTabFansOver.setVisibility(View.VISIBLE);
                ivLineTabFansPre.setVisibility(View.INVISIBLE);
                break;
            case R.id.auction_profit_layout_pre://预估到账
                auctionWithdrawalCumulativePre.setTextColor(getResources().getColor(R.color.holo_red_light));
                auctionWithdrawalCumulativeMoneyPre.setTextColor(getResources().getColor(R.color.holo_red_light));
                auctionWithdrawalCumulativeAll.setTextColor(getResources().getColor(R.color.color_font_black));
                auctionWithdrawalCumulativeMoneyAll.setTextColor(getResources().getColor(R.color.color_font_black));
                auctionWithdrawalCumulative.setTextColor(getResources().getColor(R.color.color_font_black));
                auctionWithdrawalCumulativeMoney.setTextColor(getResources().getColor(R.color.color_font_black));
                type = 0;
//                refreshLayout.autoRefresh();
                auctionRecyclerView.setVisibility(View.GONE);
                auctionRecyclerViewOver.setVisibility(View.GONE);
                auctionRecyclerViewPre.setVisibility(View.VISIBLE);
                ivLineTabFans.setVisibility(View.INVISIBLE);
                ivLineTabFansOver.setVisibility(View.INVISIBLE);
                ivLineTabFansPre.setVisibility(View.VISIBLE);
                break;
            default:
        }
    }

    @Override
    public void onLoadAuctionListByTypeSuccess(AuctionList auctionList) {

    }

    @Override
    public void onLoadAuctionListByTypeFailed() {

    }

    @Override
    public void onLoadUserCashListSuccess(AuctionProfitBean auctionProfitBean) {

    }

    @Override
    public void onLoadUserCashListFailed() {

    }

    @Override
    public void onLoadUserIncomeListSuccess(AuctionTxBean auctionTxBean) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        //可提现
        if (auctionTxBean.getUsable_income() != 0d) {
            auctionCashMoney.setText(String.format("￥%.2f", auctionTxBean.getUsable_income()));
        } else {
            auctionCashMoney.setText("￥0");
        }
        //预到账
        if (auctionTxBean.getAccountPrice() != 0d) {
            auctionWithdrawalCumulativeMoneyPre.setText(String.format("￥%.2f", auctionTxBean.getEstimatePrice()));
        } else {
            auctionWithdrawalCumulativeMoneyPre.setText("￥0");
        }
        //已到账
        if (auctionTxBean.getTotal_income() != 0) {
            auctionWithdrawalCumulativeMoney.setText(String.format("￥%.2f", auctionTxBean.getAccountPrice()));
        } else {
            auctionWithdrawalCumulativeMoney.setText("￥0");
        }
        //总收益
        if (auctionTxBean.getTotal_income() != 0) {
            auctionWithdrawalCumulativeMoneyAll.setText(String.format("￥%.2f", auctionTxBean.getTotal_income()));
        } else {
            auctionWithdrawalCumulativeMoneyAll.setText("￥0");
        }
        if (auctionTxBean.getPmUserIncomeList() != null && auctionTxBean.getPmUserIncomeList().getList() != null) {

            switch (type) {
                case 1://已到账
                    if (pageOver == 1) {
                        auctionProfitAdapterOver.setNewData(auctionTxBean.getPmUserIncomeList().getList());
                    } else {
                        auctionProfitAdapterOver.addData(auctionTxBean.getPmUserIncomeList().getList());
                    }
                    break;
                case 0://预计
                    if (pagePre == 1) {
                        auctionProfitAdapterPre.setNewData(auctionTxBean.getPmUserIncomeList().getList());
                    } else {
                        auctionProfitAdapterPre.addData(auctionTxBean.getPmUserIncomeList().getList());
                    }
                    break;
                default:
                    if (page == 1) {
                        auctionProfitAdapter.setNewData(auctionTxBean.getPmUserIncomeList().getList());
                    } else {
                        auctionProfitAdapter.addData(auctionTxBean.getPmUserIncomeList().getList());
                    }
                    break;
            }
//            if (auctionTxBean.getPmUserIncomeList().getList().size() == 0) {
//                refreshLayout.finishLoadMoreWithNoMoreData();
//            }
        } else {
//            refreshLayout.finishLoadMoreWithNoMoreData();
        }

    }

    @Override
    public void onLoadUserIncomeListFailed() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
    }

    @Override
    public void onLoadAddUserCashSuccess(String result) {

    }

    @Override
    public void onLoadAddUserCashFailed(String msg) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
